package com.lanyou.cook.repository.userManagement;

import java.util.Collection;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.Repository;
import org.springframework.data.rest.core.annotation.RestResource;

import com.lanyou.cook.entity.domain.userManagement.User;

public interface UserRepository extends Repository<User, String>, JpaSpecificationExecutor<User> {

	//用户注册验证用户账号是否已存在
	@RestResource(exported = false)
	User findByUserCode(String userCode);
	
	@RestResource(exported = false)
	List<User> findByOrganization_Id(String id);
	
	@RestResource(exported = false)
	long countByRole_Id(String roleId);
	
	@RestResource(exported = false)
	long countByFieldSystems_Id(String fieldSystemId);
	
	@RestResource(exported = false)
	List<User> findByFieldSystems_Id(String fieldSystemId);
	
	@RestResource(exported = false)
	List<User> findByUserCodeIn(Collection<String> userCodes);
	
	@RestResource(exported = false)
	List<User> findByRole_Id(String roleId);
	
	@RestResource(exported = false)
	List<User> findByUserName(String userName);
	
	@RestResource(exported = false)
	long countByUserCodeAndRole_Id(String userCode, String roleId);
	
	@RestResource(exported = false)
	long countByUserCodeAndId(String userCode, String id);
	
	@RestResource(exported = false)
	long countByRoleIsNull();
	
	
	long count();
	
	boolean exists(String id);
	
	@RestResource(exported = true)
	@RequiresPermissions(value={"userManagement"})
	<S extends User> S save(S entity);	// could expose REST API
	
	<S extends User> List<S> save(Iterable<S> entities);
	
	@RestResource(exported = true)
	@RequiresPermissions(value={"userManagement"})
	User findOne(String id);	// could expose REST API
	
	User getOne(String id);
	
	// 以下三个接口只能选一个暴露为REST API
	
	@RestResource(exported = false)
	List<User> findAll();	// could expose REST API
	
	@RestResource(exported = false)
	List<User> findAll(Sort sort);	// could expose REST API
	
	@RestResource(exported = true)
	@RequiresPermissions(value={"userManagement"})
	Page<User> findAll(Pageable pageable);	// could expose REST API
	
	List<User> findAll(Iterable<String> ids);
	
	// 以下两个接口，要么都暴露为REST API，要么都不暴露
	@RestResource(exported = true)
	@RequiresPermissions(value={"userManagement"})
	void delete(String id);	// could expose REST API
	
	@RestResource(exported = true)
	@RequiresPermissions(value={"userManagement"})
	void delete(User entity);	// could expose REST API
	
	void delete(Iterable<? extends User> entities);
	
	void deleteAll();
	
	void flush();
	
	<S extends User> S saveAndFlush(S entity);
	
	void deleteInBatch(Iterable<User> entities);
	
	void deleteAllInBatch();
	

	/*@RestResource(exported = false)
	User findByName(String name);*/
	
}
